package generator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class StrainPicker {

	private static File concatSequence;
	private static int[] picked= new int[5];
	
	public StrainPicker(){
		concatSequence = new File("src/generator/ConcatSequences.txt");
	}
	
	public String[] PickStrains(int[] strainID) throws IOException{
		picked=strainID;
		String[] sequence= new String[4];
		sequence[0]= getSequence(picked[0]);
		sequence[1]= getSequence(picked[1]);
		sequence[2]= getSequence(picked[2]);
		sequence[3]= getSequence(picked[3]);
		//System.out.println(sequence[0].length());
	
	return sequence;
		
	}
	public  String[] PickStrains() throws IOException{
		concatSequence = new File("src/strains/ConcatSequences.txt");
	//	System.out.println(concatSequence.canRead());
	//	concatSequence.
		String[] sequence= new String[4];
		for(int i=0;i<4;i++){
			picked[0]=RandomlyPickStrain();;
			picked[1]=RandomlyPickStrain();;
			picked[2]=RandomlyPickStrain();;
			picked[3]=RandomlyPickStrain();;
			
			sequence[0]= getSequence(picked[0]);
			sequence[1]= getSequence(picked[1]);
			sequence[2]= getSequence(picked[2]);
			sequence[3]= getSequence(picked[3]);
			//System.out.println(sequence[0].length());
		if(!checkStrains(sequence)[0]||!checkStrains(sequence)[1]||
					!checkStrains(sequence)[2]||
					!checkStrains(sequence)[3]){
				picked[0]=RandomlyPickStrain();;
				picked[1]=RandomlyPickStrain();;
				picked[2]=RandomlyPickStrain();;
				picked[3]=RandomlyPickStrain();;
				
				sequence[0]= getSequence(picked[0]);
				sequence[1]= getSequence(picked[1]);
				sequence[2]= getSequence(picked[2]);
				sequence[3]= getSequence(picked[3]);
//		}
		}
	//	picked=RandomlyPickStrain();
//		System.out.println("Picked "+picked);
//		String lookUp="ST "+String.valueOf(picked);
	//	String[] sequence= getSequence(picked);
		return sequence;
	}
		return sequence;
	}
	
	


	private boolean[] checkStrains(String[] sequence1) {
		boolean[] res= new boolean[4];
		res[0]=(sequence1[0].length()!=2883);
		res[1]=(sequence1[1].length()!=2883);
		res[2]=(sequence1[2].length()!=2883);
		res[3]=(sequence1[3].length()!=2883);
		return res;
	}
	/**
	 * Pick any Strain Number from 0-1029
	 * @return strain number picked
	 */
	private  int RandomlyPickStrain(){
		return (int)(Math.random()*1029);
	}
	
	
	public String getSequence(int lookUp) throws IOException {
		BufferedReader buffRead= new BufferedReader(new FileReader(concatSequence));
		String line="";
		int lineNum=lookUp*2;
		int count=0;
		while(lineNum!=count){line=buffRead.readLine();count++;}//System.out.println(line);}
		//line=buffRead.readLine();
	//	line=buffRead.readLine();
		return line;
	}


	public void printtestSTrains(){
		for(int i=0;i<50;i++){
			picked[i]=RandomlyPickStrain();
			System.out.print(picked[i]+",");
		}
	}

	public int[] getPicked() {
		return picked;
	}
	
	

}
